﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Events;
using Services;
using CommonModule;
using Microsoft.Practices.ServiceLocation;

namespace HeaderContent
{
    public class HeaderViewModel : ViewModelBase
    {
        public ICommand LoginCommand { get; set; }
        public ICommand LogoutCommand { get; set; }
        IEventAggregator EvtAggregator;
        bool loggedVisible;
        bool loggedOutVisible;

        public bool LoginButtonVisible
        {
            get
            {
                return this.loggedVisible;
            }
            set
            {
                this.loggedVisible = value;
                FireNotifaction("LoginButtonVisible");
            }
        }

        public bool LoggedOutButtonVisible
        {
            get
            {
                return this.loggedOutVisible;
            }
            set
            {
                this.loggedOutVisible = value;
                FireNotifaction("LoggedOutButtonVisible");
            }
        }
        


        public HeaderViewModel()
        {
            this.LoginButtonVisible = true;
            this.LoggedOutButtonVisible = false;
            CustomInitialise();
        }

        private void CustomInitialise()
        {
            this.LoginCommand = new DelegateCommand<object>(OnLoginClicked);
            this.LogoutCommand = new DelegateCommand<object>(OnLogOut);
            this.EvtAggregator = SingletonUnityService.Instance.GetByType<IEventAggregator>();            
            this.EvtAggregator.GetEvent<CredentialValidateEventArgs>().Subscribe(OnCredentialValiation);
        }

        void OnLoginClicked(object parm)
        {
            this.EvtAggregator.GetEvent<BaseEventArgs>().Publish(null);
        }

        void OnLogOut(object parm)
        {
            this.LoginButtonVisible = true;
            this.LoggedOutButtonVisible = false;
            SingletonRegionService.Instance.RemoveAllViewsFromRegion(Regions.Content);
            
        }

        public void OnCredentialValiation(UserCredential e)
        {
            if (e.UserName.Equals("suren"))
            {
                this.LoginButtonVisible = false;
                this.LoggedOutButtonVisible = true;
                SingletonRegionService.Instance.RemoveViewFromRegion(Regions.Content, "WelcomeView");
                SingletonRegionService.Instance.ActivateViewInRegion(Regions.Content, "LoggedInView");
            }

        }

    }
}
